<?php

namespace myshop\common\dao;

use longhu\base\BaseDao;
use myshop\system\model\Permissions;
use think\exception\ValidateException;

class PermissionsDao extends BaseDao
{
    public function setModel():string
    {
        return Permissions::class;
    }


    /**
     * @param int $parent_id
     * @param array $data
     * @return true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function createByParent(int $parent_id = 0,array $data = [])
    {
        $level = '';
       if($parent_id){
           $parentInfo = $this->getModel()->findBy($parent_id);
           if(empty($parentInfo)){
               throw new ValidateException("上级菜单不存在");
           }
           $level = $parentInfo->level;
       }
        $data['parent_id'] = $parent_id;
        $menu_id = $this->create($data);
        if($menu_id){
            if(empty($level)){
                $level = $menu_id;
            }else{
                $level = $level.'-'.$menu_id;
            }
            $this->updateById($menu_id,[
                'level'=>$level
            ]);
        }

        return true;
    }


}